<?php

namespace App\Models;

//use Laravel\Scout\Searchable;
use App\Scopes\ApplyScope;
use Illuminate\Database\Eloquent\Model;

class Apply extends Model
{
    protected $connection = 'mysql_utf8mb4';

    //暂时不使用全文搜索，队列未配置好
//    use Searchable;

    const STAGE_1 = 1;//初审
    const STAGE_2 = 2;//复审
    const STAGE_3 = 3;//三审

    public static $stageType = [
        self::STAGE_1 => '初审',
        self::STAGE_2 => '复审',
        self::STAGE_3 => '终审',
    ];
    /**
     * 是否为终审
     * @param $apply object
     * @return bool
     */
    public static function isLastStage($apply)
    {
        return (in_array($apply->apply, self::$thinkTank) && $apply->stage == self::STAGE_2)
            || $apply->stage == self::STAGE_3;
    }

    protected $table = 'apply';

    protected $guarded = [];

    //审核状态
    const STATUS_UNREAD = 0;
    const STATUS_UNDETERMINED = 1;
    const STATUS_PASS = 2;
    const STATUS_REFUSE = 3;
    public static $status = [
        self::STATUS_UNREAD => '未查看',
        self::STATUS_UNDETERMINED => '待定',
        self::STATUS_PASS => '通过',
        self::STATUS_REFUSE => '拒绝',
    ];

    //岗位,数字不可改变
    public static $applyType = [
        1 => '活动策划',
        2 => '内容运营',
        3 => '设计师',
        4 => '摄影师',
        5 => '智库开发组',
        6 => '智库视频组',
        7 => '高级活动策划',
        8 => '高级内容运营',
        9 => '高级设计师',
        10 => '高级摄影师',
        11 => '智库策划组',
        12 => '智库运营组',
        13 => '智库设计组',
        14 => '智库摄影组',
    ];
    //智库
    public static $thinkTank = [5, 6, 11, 12, 13, 14];
    //非智库
    public static $notThinkTank = [1, 2, 3, 4, 7, 8, 9, 10];

    /**
     * 根据年级获得可以选择的岗位
     * @param $grade
     * @return array
     */
    public static function getApply($grade)
    {
        $grade = intval($grade);
        switch ($grade) {
            case 1:
                return [1, 2, 3, 4, 5, 6];
            case 2:
                return [7, 8, 9, 10, 5, 6];
            default:
                return [11, 12, 13, 14, 5, 6];
        }
    }

    /**
     * 数据模型的启动方法
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new ApplyScope);
    }

    public function schoolObj()
    {
        return $this->belongsTo('App\Models\School', 'school', 'name');
    }

    /**
     * 审核内容
     */
    public function reviews()
    {
        return $this->hasMany('App\Models\ApplyReview');
    }

    /**
     * 查询未审核的报名表单
     * @param $query
     * @return mixed
     */
    public function scopeNotReview($query)
    {
        $user = getUser();
        $stages = [];
        $start = strlen(Permission::STAGE_PRE);
        foreach ($user->getPermissions() as $userPermission) {
            if (starts_with($userPermission->slug, Permission::STAGE_PRE)) {
                $stages[] = substr($userPermission->slug, $start);
            }
        }
        return $query->whereIn('status', [self::STATUS_UNREAD, self::STATUS_UNDETERMINED])->whereIn('stage', $stages);
    }
}
